package com.shen.server.service.impl;

import com.shen.server.pojo.Admin;
import com.shen.server.pojo.Menu;
import com.shen.server.mapper.MenuMapper;
import com.shen.server.service.IMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author HMF
 * @since 2022-04-10
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements IMenuService {

    @Resource
    private MenuMapper menuMapper;
    @Autowired
    private RedisTemplate<String,Object> redisTemplate;
    /**
     * 根据用户id查找用户的菜单列表
     * @date: 2022/4/15
     * @return
     */
    @Override
    public List<Menu> getMeauByAdminId() {
        Integer adminID = ((Admin) SecurityContextHolder.getContext()
                .getAuthentication().getPrincipal()).getId();
        List<Menu> menus = menuMapper.getMeauByAdminId(adminID);
        /*ValueOperations<String,Object> valueOperations = redisTemplate.opsForValue();
        //从redis获取菜单数据
        List<Menu> menus = (List<Menu>) valueOperations.get("menu_"+adminID);
        if(CollectionUtils.isEmpty(menus)){
            //redis中没有则通过数据库里去查找
            menus =  menuMapper.getMeauByAdminId(adminID);
            //给redis里存入相应的缓存
            valueOperations.set("menu_"+adminID,menus);
        }*/
        return menus;
    }

    /**
     * 根据角色获取菜单列表
    * @date: 2022/4/15
    * @return
    */

    @Override
    public List<Menu> getMenusWithRole() {
        return menuMapper.getMenusWithRole();

    }

    /**
     * 查询所有菜单（包括子菜单）
    * @date: 2022/4/17
    * @return
    */

    @Override
    public List<Menu> getAllMenus() {

        return menuMapper.getAllMenus();
    }

}
